﻿Chenxin.UI.InfoPulishPage = Chenxin.extend(Chenxin.UI.AddPageCommon, {
    actionUrl: '/service/infoPulish.ashx',
    createForm: function() {
        return new Chenxin.UI.InfoPulishForm({
            scope: this
        });
    },
    customerInit: function() {
        this.editor = $('#txtrContent').xheditor();
    },
    initEvent: function() {
        var objPage = this;
        /*************************事件：月份得到焦点************************/
        $("#txtPublishTime").focus(function() {
            WdatePicker({ dateFmt: 'yyyy-MM-dd', oncleared: function() { $(this).blur(); }, onpicked: function() { $(this).blur(); } })
        });

        var isComplete = true;
        var hasFile = false;
        /*************************事件：发布信息************************/
        $("#aInfoPublish").click(function() {
            $("#FileUploadify").uploadifyUpload();
            if ( isComplete && !hasFile ) {
                objPage.form.save();
            }
        });
        /*************************事件：删除信息************************/
        $("#divBtDel").click(function() {
            objPage.form.op = "del";
            objPage.form.currentId = objPage.form.currentModel.RecordId;
            objPage.form.del();
        });

        /**************事件：删除文件*************/
        $("#chkFile").click(function() {
            if ($(this).attr("checked") == false) {
                if (confirm("确定删除附件吗？")) {

                } else {
                    $(this).attr("checked", true);
                }
            } else {
                if (objPage.form.fileNames.length > 0) {
                    alert("请选择一个附件进行删除！");
                    $(this).attr("checked", false);
                }
            }
        });

        /*************************事件：返回一览************************/
        $("#divBtRet").click(function() {
            objPage.retPage(objPage.form.currentModel.InfoType);
        });

        //文件上传
        $("#FileUploadify").uploadify({
            uploader: '/js/uploadify.swf',
            script: '/service/infoPublish.ashx?op=fileupload',
            cancelImg: '/images/cancel.png',
            buttonImg: '/images/fileupload.png',
            auto: false,
            multi: false,
            height: 24,
            width: 190,
            sizeLimit: 1048576,
            onSelect: function(event, queueID, fileObj) {
                $("#chkFile").attr("checked", false);
                hasFile = true;
            },
            onSelectCancel: function() {
                if (!hasFile) {
                    isComplete = true;
                }
            },
            onClick: function() {
                if (!hasFile) {
                    isComplete = false;
                }
            },
            onComplete: function(event, queueID, fileObj, response, data) {
                if (response) {
                    try {
                        var json = eval("(" + response + ")")
                        if (json.success == 1) {
                            if (data.fileCount == 0) {
                                objPage.form.fileNames.push(json.data.filename);
                                objPage.form.saveNames.push(json.data.savename);
                                objPage.form.save();
                                objPage.form.fileNames.length = 0;
                                objPage.form.saveNames.length = 0;
                                isComplete = true;
                                hasFile = false;
                            }
                        } else if (json.success == 0) {
                            alert(json.msg);
                        } else {
                            alert(json.msg);
                            window.location.href = Chenxin.loginUrl;
                        }
                    } catch (e) {
                        alert('返回数据格式错误，文件上传失败！');
                    }
                }
            },
            onError: function(event, queueID, fileObj) {
                alert("文件上传失败");
            },
            onCancel: function(event, queueID, fileObj) {
                isComplete = true;
                hasFile = false;
            }
        });
    },
    retPage: function(InfoType) {
        var ToUrl;
        ToUrl = "PublicInfo.aspx?Type=" + InfoType;
        window.location.href = ToUrl;
    }
});

Chenxin.UI.InfoPulishForm = Chenxin.extend(Chenxin.UI.AddFormCommon,{
    id:"divForm",
    actionUrl:'/service/infoPublish.ashx',
    op:"publish",
    uploadFile:true,
    createComboList:function(){
        var objFrm = this;
        //信息类型
	    var selInfoType = new Chenxin.UI.Combo({
            containerId:'#InfoType',
            colWidth:90,
            actionUrl: '/service/infoPublish.ashx',
            param:{op:"getInfoType"},
            autoLoad:true,
            onSelected:function(){
                //特别信息
                if( selInfoType.getText() == "特别信息" ){
                    $("#trSetPopUp").show();
                }else{
                    $("#trSetPopUp").hide();
                    $("#chkIsPopUp").attr("checked",false);
                }
            },
            afterSetValue:function(){
                //特别信息
                if( selInfoType.getText() == "特别信息" ){
                    $("#trSetPopUp").show();
                }else{
                    $("#trSetPopUp").hide();
                    $("#chkIsPopUp").attr("checked",false);
                }
            },
            afterInit:function(){
                if( objFrm.currentModel !=undefined ){
                    this.setValue(objFrm.currentModel.InfoType);
                }
            }
	    });
	    return [selInfoType];
    },
    afterGetFormJson:function(json){
        json.Content = escape(this.editor.getSource());
        if(json.chkFile != undefined){
            delete json.chkFile;
        }
        if($("#chkFile").attr("checked") == false){
            this.form.additionalParam.uploadfl = $("#chkFile").val();
        }
    },
    manualReset:function(){
        this.scope.editor.setSource('');
    },
    initForm:function(){
        if($("#ctl00_main_hidInfo").val() != ""){
            var model = eval("(" + $("#ctl00_main_hidInfo").val() + ")");
            model.PublishTime = model.PublishTime.split(" ")[0];
            this.currentModel = model;
        }
    },
    loadDefVal:function(){
        if( this.currentModel != undefined ){
            Chenxin.Fn.setFormValue(this.id,this.comboList,this.currentModel);
            $("#divBtDel").show();
            $("#divBtRet").show();
            $("#chkFile").attr("checked",true);
        }
    },
    saveSucc:function(){
        
        if( this.form.currentModel != undefined ){
            this.retPage(this.form.currentModel.InfoType);
        }
    },
    delSucc:function(){
        if( this.form.currentModel != undefined ){
            this.retPage(this.form.currentModel.InfoType);
        }
    },
    manualPreCheck:function(){
        //信息类型
        if( this.comboList[0].getValue() == "请选择" ){
            alert("请选择信息类型");
            return false;
        }
        //发布时间
        if($("#txtPublishTime").val() == ""){
            alert("请输入发布时间");
            $("#txtPublishTime").focus();
            return false;
        }
        //主题
        if($("#txtSubject").val() == ""){
            alert("请输入信息主题");
            $("#txtSubject").focus();
            return false;
        }
    }
});

$(document).ready(function(){
   var page = new Chenxin.UI.InfoPulishPage();
});
